from google.appengine.ext import db
import decimal

class Registration(db.Model):
    """Models an individual Registration_Book entry with an author, content, and date."""
    first_name = db.StringProperty()
    last_name = db.StringProperty()
    street_address = db.PostalAddressProperty()
    city = db.StringProperty()
    state = db.StringProperty()
    zip_code = db.StringProperty()
    email = db.EmailProperty()
    phone_number = db.PhoneNumberProperty()
    author = db.StringProperty()
    temp_user_key = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)
    
    @classmethod
    def get_by_temp_key(cls, key):
        cls.all().filter("temp_user_key ==", key).get()
    
class Temp_User(db.Model):
    """Models an temporary user information object"""
    first_name = db.StringProperty()
    last_name = db.StringProperty()
    street_address = db.PostalAddressProperty()
    city = db.StringProperty()
    state = db.StringProperty()
    zip_code = db.StringProperty()
    email = db.EmailProperty()
    phone_number = db.PhoneNumberProperty()
    author = db.StringProperty()
    date = db.DateTimeProperty(auto_now_add=True)
    
class LocationStats(db.Model):
    city = db.StringProperty()
    state = db.StringProperty()
    count = db.IntegerProperty()
    
    @classmethod
    def add_loc(cls, city, state):
        loc = cls.all().filter('city ==', city).filter('state ==', state)
        loc = loc.get()
        if loc is None:
            loc = cls(city=city, state=state, count=1)
        else:
            loc.count = loc.count + 1
        loc.put()
        return loc

class Profile(db.Model):
    '''extra user details'''
    owner = db.UserProperty()
    paypal_email = db.EmailProperty()  # for payment
    
    @staticmethod
    def from_user( u ):
        return Profile.all().filter( "owner = ", u ).get()

class Item(db.Model):
    '''an item for sale'''
    owner = db.UserProperty()
    created = db.DateTimeProperty(auto_now_add=True)
    title = db.StringProperty()
    price = db.IntegerProperty() # cents
    
    def price_dollars( self ):
        return self.price / 100.0
    
    def price_decimal( self ):
        return decimal.Decimal( str( self.price / 100.0 ) )
    
    @classmethod
    def get_by_title(cls, title):
        cls.all().filter(title=title).run()
    @staticmethod
    def recent():
        return Item.all().filter( "enabled =", True ).order('-created').fetch(10)
 
class Purchase(db.Model):
    '''a completed transaction'''
    item = db.ReferenceProperty(Item)
    owner = db.UserProperty()
    purchaser = db.UserProperty()
    created = db.DateTimeProperty(auto_now_add=True)
    status = db.StringProperty( choices=( 'NEW', 'CREATED', 'ERROR', 'CANCELLED', 'RETURNED', 'COMPLETED' ) )
    status_detail = db.StringProperty()
    secret = db.StringProperty() # to verify return_url
    debug_request = db.TextProperty()
    debug_response = db.TextProperty()
    paykey = db.StringProperty()
    shipping = db.TextProperty()
